<?php

class MakeNewPostController {
	private $m_makeNewPostView = "";
	private $m_observerController = "";	
	private $m_observerHandler = "";
	private $m_validate ="";
	private $m_navigationView = "";
		
	public function __construct ($a_makeNewPostView, $a_observerController, $a_observerHandler, $a_validate, $a_navigationView){
		$this->m_makeNewPostView = $a_makeNewPostView;
		$this->m_observerController = $a_observerController;
		$this->m_observerHandler = $a_observerHandler;
		$this->m_validate = $a_validate;
		$this->m_navigationView = $a_navigationView;
	}
	
	public function DoControl () {
		$xhtml = "";
		//You can use these tags in your new posts
		$allowedTagsContent = "<p><a><ul><li>ol<img><br>";
		
		if($this->m_makeNewPostView->TriedToUploadPost()){
			$newPostContent = $this->m_makeNewPostView->GetNewPostContents();
			
			//Remove tags that are not allowed. 
			$header = $this->m_validate->removeAllTags($_POST[ObserverView::POST_HEADER]);
			$content = $this->m_validate->removeSomeTags($_POST[ObserverView::POST_CONTENT], $allowedTagsContent);
			$category = $this->m_validate->removeAllTags($_POST[ObserverView::CATEGORYID]);
			$category = intval($category);
			
			//are you sure it's an int coming in from categorys? Let's check. 
			if(is_int($category)){
				//Naow we make our new post. But what should happen then? At this moment, headerlocation. not sure about that.. 
				$this->m_observerHandler->NewPost($header, $content, $category);
				$this->m_navigationView->NavigateToPost ($category, ObserverView::CATEGORYID_URLPARAMETER);
			}//Annars så.... 
		}else {
			//Get the newpost-form
			$categoryArray = $this->m_observerController->GetCategoryArray();
			$xhtml .= $this->m_makeNewPostView->GetForm($categoryArray);
		
		}
		return $xhtml;
	}
	
}